import { fileURLToPath } from "node:url";
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";

import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

import path from "path";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import UnoCSS from "unocss/vite";
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd());
  const environmentName =
    env.VITE_ENVIRONMENT_FILE_FULL_NAME || "environment.dev-default";

  return {
    base: "",
    server: {
      port: 6690,
    },
    plugins: [
      vue(),
      UnoCSS(),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), "src/assets/icons/svg")],
        // 指定 symbolId 的格式
        symbolId: "icon-[name]",
      }),
      AutoImport({
        imports: ["vue"],
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
        dirs: ["./src/components", "./src/pages/components"],
      }),
    ],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
        "vite-alias--app-environment": fileURLToPath(
          new URL(`./src/app-environments/${environmentName}`, import.meta.url)
        ),
        "vite-alias--app-settings": fileURLToPath(
          new URL(`./src/app-settings/index.ts`, import.meta.url)
        ),
      },
    },
  };
});
